import { getAlarmDetail } from '@/api/alarm'
import dayjs from 'dayjs'
import { useEffect, useState } from 'react'
export function isValidKey(key: string | number | symbol, object: object): key is keyof typeof object {
	return key in object
}
const useDetailHook = (moid: string) => {
	const [detailData, setDetailData] = useState([{ label: '', value: '', valueName: `` }])
	let count = 1
	useEffect(() => {
		getDetailInfo()
		// eslint-disable-next-line react-hooks/exhaustive-deps
	}, [count])
	/**获取详情信息 */
	const getDetailInfo = () => {
		let params = {
			moid: moid,
		}
		getAlarmDetail(params).then((res) => {
			if (res.data) {
				detailChange(res.data)
			}
			// setDetailData(res.data)
		})
	}
	const detailChange = (rowData: any) => {
		let list = []
		let changeObject = {
			account_login: '登录账号',
			kind: '行为类别',
			level: '风险级别',
			description: '事件描述',
			event_name: '事件名称',
			type: '事件类型',
			created_at: '事件时间',
			extenslons: '事件相关人员信息',
			company: '厂商',
			name: '名称',
		}
		for (let key in changeObject) {
			if (isValidKey(key, changeObject)) {
				let valueName = `value`
				let reValue = null
				if (key === 'created_at') {
					reValue = dayjs(rowData[key]).format('YYYY-MM-DD HH:mm:ss')
				} else if (key === 'kind') {
					let kindList = ['-', '异常', '违规']
					reValue = kindList[rowData[key]]
					valueName = valueName + ` alarm_status_${rowData[key]}`
				} else if (key === 'level') {
					let levelList = ['紧急', '警报', '关键', '错误', '告警', '通知', '信息', '调试']
					reValue = levelList[rowData[key]]
				} else if (key === 'extenslons') {
					reValue = rowData[key] === '{}' ? '-' : rowData[key]
				} else {
					reValue = rowData[key]
				}
				list.push({ label: changeObject[key], value: reValue, valueName: valueName })
			}
		}
		setDetailData(list)
	}

	return { detailData }
}
export default useDetailHook
